package com.wx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.wx.dao.SysWebArticleDao;
import com.wx.dao.SysWebArticleMapper;
import com.wx.dao.SysWebSiteMapper;
import com.wx.entity.SysWebArticle;
import com.wx.entity.SysWebArticleExample;
import com.wx.entity.SysWebSite;
import com.wx.entity.SysWebSiteExample;
import com.wx.model.article.ArticleSummaryRecordModel;
import com.wx.model.article.SiteModel;
import com.wx.service.ArticlemsgService;
import com.wx.service.SaveArticleParameter;
import com.zyht.common.util.Page;

@Service
public class ArticlemsgServiceImpl implements ArticlemsgService {
	
	@Resource
	private SysWebArticleMapper sysWebArticleMapper;
	
	@Resource
	private SysWebSiteMapper sysWebSiteMapper;
	
	@Resource
	private SysWebArticleDao sysWebArticleDao;
	
	@Override
	public Page<SysWebArticle> getListByPage(Integer currentPage, Long siteid) {
		SysWebArticleExample example = new SysWebArticleExample();
		example.setOrderByClause(" id desc");
		if(siteid!=null){
			example.createCriteria().andSite_idEqualTo(siteid);
		}
		
		PageHelper.startPage(currentPage, 10);
		com.github.pagehelper.Page<SysWebArticle> list = (com.github.pagehelper.Page<SysWebArticle>) sysWebArticleMapper .selectByExampleWithBLOBs(example);
		
		//栏目名
		SysWebSite site = null;
		for (SysWebArticle article : list) {
			site = sysWebSiteMapper.selectByPrimaryKey(article.getSite_id());
			if(site!=null){
				article.setSite_name(site.getName());
			}else{
				article.setSite_name("栏目不存在");
			}
		}
		
		Page<SysWebArticle> page = new Page<>((int)list.getTotal(), currentPage, 10);
		page.setRecordList(list.getResult());
		return page;
	}
	
	@Override
	public Page<SysWebArticle> getListByPage(Integer currentPage) {
		SysWebArticleExample example = new SysWebArticleExample();
		example.setOrderByClause(" id desc");
		int total = sysWebArticleMapper.countByExample(example);

		Page<SysWebArticle> page = new Page<SysWebArticle>(total, currentPage, 10);
		example.setOrderByClause(example.getOrderByClause() + " limit " + page.getStart() + " , " + page.getPernum());

		List<SysWebArticle> list = sysWebArticleMapper .selectByExampleWithBLOBs(example);
		
		if (list == null) {
			list = new ArrayList<SysWebArticle>();
		}
		//栏目名
		SysWebSite site = null;
		for (SysWebArticle article : list) {
			site = sysWebSiteMapper.selectByPrimaryKey(article.getSite_id());
			if(site!=null){
				article.setSite_name(site.getName());
			}else{
				article.setSite_name("栏目不存在");
			}
		}
		page.setRecordList(list);
		return page;
	}

	@Override
	public void doSaveArticle(SaveArticleParameter p, Long id) {
		SysWebArticle article= new  SysWebArticle();
		article.setName(p.name);
		article.setSite_id(p.site_id);
		article.setLitpic(p.litpic);
		article.setSource(p.source);
		article.setPublish(p.publish);
		article.setAuthor(p.author);
		article.setSummary(p.summary);
		article.setContent(p.content);
		article.setOrder(p.order);
		if(id==null){
			article.setAddtime(new Date());
			sysWebArticleMapper.insertSelective(article);
		}else{
			article.setId(id);
			sysWebArticleMapper.updateByPrimaryKeySelective(article);
		}
	}

	@Override
	public SysWebArticle getArticle(Long article_id) {
		return sysWebArticleMapper.selectByPrimaryKey(article_id);
	}

	@Override
	public List<SiteModel> getSite() {
		return sysWebArticleDao.getSiteModel();
	}

	@Override
	public List<ArticleSummaryRecordModel> getArticleSummary(Long sitenId, Integer currentPage,Integer rowPage) {
		if(currentPage==null || currentPage<=0){
			currentPage = 1;
		}
		if(rowPage==null || rowPage<=0){
			rowPage = 20;
		}
		return sysWebArticleDao.getArticleSummary( sitenId, (currentPage-1)*rowPage,rowPage);
	}

	@Override
	public SysWebSite getWebSiteBySiteId(Long siteId) {
		return sysWebSiteMapper.selectByPrimaryKey(siteId);
	}

	@Override
	public List<SysWebSite> getSitePid(Long siteId) {
		SysWebSiteExample example = new SysWebSiteExample();
		example.createCriteria().andPidEqualTo(siteId);
		List<SysWebSite>  l = sysWebSiteMapper.selectByExample(example);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return l ;
		}
	}
	
	@Override
	public List<SysWebSite> getSitePids(List<Long> siteIds) {
		SysWebSiteExample example = new SysWebSiteExample();
		example.createCriteria().andPidIn(siteIds);
		List<SysWebSite>  l = sysWebSiteMapper.selectByExample(example);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return l ;
		}
	}

	@Override
	public Integer deleteArticleById(Long id){

		return this.sysWebArticleMapper.deleteByPrimaryKey(Long.valueOf(id));
	}

	@Override
	public SysWebArticle getArticleBySiteIdFT(Long siteId) {
		return sysWebArticleDao.getArticleFT(siteId);
	}

	@Override
	public List<SysWebArticle> list() {
		// TODO Auto-generated method stub
		return this.sysWebArticleDao.list();
	}
	
}
